<%-- 
    Document   : productList
    Created on : 2019-9-11, 10:08:14
    Author     : danny
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@include file="/WEB-INF/jspf/top.jsp" %>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>商品列表</title>
    </head>
    <body>
        <a href="${contextPath}/product.html?action=addForm">新增</a>
        <c:if test="${empty resultList}">
            <h1>目前没有任何商品</h1>
        </c:if>
        <c:if test="${not empty resultList}"> 
            <table>
                <tr>
                    <th></th>
                    <th></th>
                    <th>商品名称</th>
                    <th>商品描述</th>
                    <th>商品库存</th>
                    <th>商品价格</th>
                    <th>操作</th>
                </tr>
                <c:forEach items="${resultList}" var="p" varStatus="status">
                    <tr>
                        <td><img src="${contextPath}/files/${p.epFileName}" alt="no pic"/></td>
                        <td>${status.index+1}</td>
                        <td>${p.epName}</td>
                        <td>${p.epDescription}</td>
                        <td>${p.epStock}</td>
                        <td>${p.epPrice}</td>
                        <td><a href="javascript:addToCart(${p.epId})">添加到购物车</a></td>
                    </tr>
                </c:forEach>
            </table>
            <!--分页控件-->
            <form action="${contextPath}/product.html?action=list" id="pageForm">
                <input type="hidden" value="${pager.currentPage}" id="page" name="page"/>
                <input type="hidden" value="list" name="action"/>
                每页
                <select name="row">
                    <option value="3" <c:if test="${pager.rowPerPage eq 3}">selected</c:if>>3</option>
                    <option value="5" <c:if test="${pager.rowPerPage eq 5}">selected</c:if>>5</option>
                    <option value="10" <c:if test="${pager.rowPerPage eq 10}">selected</c:if>>10</option>
                </select>
                条
                &nbsp;&nbsp;&nbsp;&nbsp;
                共 ${pager.rowCount}
                &nbsp;&nbsp;&nbsp;&nbsp;
                <a href="javascript:pre()">上一页</a>
                &nbsp;&nbsp;
                当前第 ${pager.currentPage} 页
                &nbsp;&nbsp;
                <a href="javascript:next()">下一页</a>
                &nbsp;&nbsp;&nbsp;&nbsp;
            </form>
        </c:if>  
    </body>
    <script>
        function pre() {
            var p = document.getElementById("page").value;
            if (p>1) {
                var pg = parseInt(p);
                document.getElementById("page").value = p - 1;
            }
            console.log(p);
            document.getElementById("pageForm").submit();
        }
        function next() {
            var p = document.getElementById("page").value;
            if (p<${pager.pageCount}) {
                var pg = parseInt(p);
                document.getElementById("page").value = pg + 1;
            }
            console.log(p);
            document.getElementById("pageForm").submit();
        }
        
        function addToCart(epId) {
            location.href = "${contextPath}/cart?action=add&prodId="+epId;
        }
    </script>
</html>
